package br.com.ngame.manager;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Window;
import java.io.File;
import java.util.HashMap;
import java.util.Vector;

public class WindowedSoundManager implements SoundManager
{

	protected HashMap<String, Vector<AudioClip>> collections;
	protected Window window;
	private boolean soundsInResource = true;

	public WindowedSoundManager(Window win)
	{
		window = win;
		collections = new HashMap<String, Vector<AudioClip>>();
	}

	public void loadSoundCollections(String[] fileNames)
	{
		for (int i = 0; i < fileNames.length; i++)
		{
			loadSoundCollection(fileNames[i]);
		}
	}

	public void loadSoundCollection(String fileName)
	{
		loadSound(fileName);
	}

	public void loadSound(String fileName)
	{
		Vector<AudioClip> vect = new Vector<AudioClip>();
		AudioClip sound = null;
		try
		{
			if (isSoundsInResource())
			{
				sound = Applet.newAudioClip(getClass().getResource("/" + fileName));
			} else
			{
				File file = new File(fileName);
				if (file.exists())
				{
					sound = Applet.newAudioClip(file.toURI().toURL());
				}
			}
		} catch (Exception e)
		{
			System.err.println("[loadSound] " + e.getClass().getName() + ": " + e.getMessage());
		}
		vect.add(sound);
		collections.put(fileName, vect);
	}

	public AudioClip getSound(String fileName, int soundIndex)
	{
		return collections.get(fileName).get(soundIndex);
	}

	public AudioClip getSound(String fileName)
	{
		return collections.get(fileName).get(0);
	}

	public int getSoundCount(String fileName)
	{
		return collections.get(fileName).size();
	}

	public boolean isSoundsInResource() {
        return soundsInResource;
    }

    public void setSoundsInResource(boolean value) {
        soundsInResource = value;
    }
}
